package com.example.yls.demoa;

import android.app.Application;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
        MapView mMapView;
        BaiduMap mBaiduMap;
        LocationClient mLocationClient;
        MyLocationConfiguration mLocationConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView=findViewById(R.id.baidu_mapview);
        mBaiduMap = mMapView.getMap();

        mBaiduMap.setMyLocationEnabled(true);
//定位初始化
        mLocationClient=new LocationClient(this);
         mLocationConfiguration=new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING
               , false,
                BitmapDescriptorFactory.fromResource(R.drawable.timg),
                0xAAFFFF88,
                0xAA00FF00);

        mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);
//长按
        mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,"长按 纬度："+
                        latLng.latitude+"经度"+latLng.longitude,Toast.LENGTH_SHORT).show();
                //绘制marker图标
                /*createMarker(latLng);*/
                //绘制折线
//                PolylineOptions(latLng);
//                绘制弧线
//                ArcOptions(latLng);
//            绘制圆
                CircleOptions(latLng);
            
            }
        });

        //双击事件
        mBaiduMap.setOnMapDoubleClickListener(new BaiduMap.OnMapDoubleClickListener() {
            @Override
            public void onMapDoubleClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,"双击 纬度："+
                        latLng.latitude+"经度"+latLng.longitude,Toast.LENGTH_SHORT).show();

            }
        });

//单击
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        Toast.makeText(MainActivity.this,"单击 纬度："+
                latLng.latitude+"经度"+latLng.longitude,Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onMapPoiClick(MapPoi mapPoi) {
        Toast.makeText(MainActivity.this,"poi单击"+mapPoi.getName()+"坐标"
                +mapPoi.getPosition().toString(),Toast.LENGTH_SHORT).show();

        return false;
    }
});


//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

//设置locationClientOption
        mLocationClient.setLocOption(option);





//注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();

    }

    private void CircleOptions(LatLng latLng) {
        LatLng center = latLng;

//构造CircleOptions对象
        CircleOptions mCircleOptions = new CircleOptions().center(center)
                .radius(1400)
                .fillColor(0xAA0000FF) //填充颜色
                .stroke(new Stroke(5, 0xAA00ff00)); //边框宽和边框颜色

//在地图上显示圆
        Overlay mCircle = mBaiduMap.addOverlay(mCircleOptions);
    }

    private void ArcOptions(LatLng latLng) {

        LatLng p1 = new LatLng(latLng.latitude, latLng.longitude);//起点
        LatLng p2 = new LatLng(latLng.latitude-0.05, latLng.longitude+0.05);//中间点
        LatLng p3 = new LatLng(latLng.latitude, latLng.longitude+0.1);//终点

//构造ArcOptions对象
        OverlayOptions mArcOptions = new ArcOptions()
                .color(Color.RED)
                .width(10)
                .points(p1, p2, p3);

//在地图上显示弧线
        Overlay mArc = mBaiduMap.addOverlay(mArcOptions);
    }

    private void PolylineOptions(LatLng latLng) {
        LatLng p1 = new LatLng(latLng.latitude, latLng.longitude);
        LatLng p2 = new LatLng(latLng.latitude-0.05, latLng.longitude+0.05);
        LatLng p3 = new LatLng(latLng.latitude, latLng.longitude+0.1);
        List<LatLng> points = new ArrayList<LatLng>();
        points.add(p1);
        points.add(p2);
        points.add(p3);

//设置折线的属性
        OverlayOptions mOverlayOptions = new PolylineOptions()
                .width(10)
                .color(0xAAFF0000)
                .points(points);
//在地图上绘制折线
//mPloyline 折线对象
        Overlay mPolyline = mBaiduMap.addOverlay(mOverlayOptions);
    }

    private void createMarker(LatLng latLng) {
        LatLng point=latLng;
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.marker);
//构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
//在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
    }

    class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
        }
    }
    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mLocationClient.stop();
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }

}
